package video

// 分类所属的内容，包括视频和视频包
type Content struct {
	CateID int64 `json:"cate_id"`
	Type       string `json:"type" xorm:"assoc_type"`
	ID         int64  `json:"id"`
	Title      string `json:"title"`
	Picture    string `json:"picture"`
	TeacherID  int64  `json:"teacher_id"`
	Content    string `json:"content"`
	Hits       int    `json:"hits"`   // 点击量
	Weight     int    `json:"weight"` // 权重
	CreateTime int64  `json:"create_time"`
}

func SelectContentByCate(cid int64) (data []Content, err error) {
	data = make([]Content, 0)
	query := "SELECT a.cate_id, a.assoc_type, a.weight, v.id, v.title, v.picture, v.teacher_id, v.content, v.hits, v.create_time " +
		"FROM t_video_cate_assoc AS a JOIN t_video AS v ON v.id = a.assoc_id WHERE a.assoc_type = 'video' UNION "+
		"SELECT a.cate_id, a.assoc_type, a.weight, p.id, p.title, p.picture, p.teacher_id, p.content, p.hits, p.create_time " +
		"FROM t_video_cate_assoc AS a JOIN t_video_pack AS p ON p.id = a.assoc_id WHERE a.assoc_type = 'pack'"
	if err = rx.SQL("SELECT * FROM ("+query+") AS a WHERE cate_id = ? ORDER BY weight DESC", cid).Find(&data); err != nil {
		return
	}
	return
}